﻿using System;
using System.Windows.Forms;
using TasksDataXml.Data;

namespace DropBoxToDo.UserControls
{
    public partial class TaskControl : UserControl
    {
        private Task _data = new Task();
        public Func<Task, bool> OnTaskDone { get; set; }
        public Func<Task, bool> OnTaskClick { get; set; }

        public Task Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public Guid TaskId { get { return _data.GuidId; } set { _data.GuidId = value; } }
        public string Desc { get { return _data.Desc; } set { _data.Desc = value; } }
        public string Note { get { return _data.Note; } set { _data.Note = value; } }
        public DateTime IssueDate { get { return _data.IssueDate; } set { _data.IssueDate = value; } }
        public bool IsCompleted { get { return _data.IsCompleted; } set { _data.IsCompleted = value; } }

        public TaskControl()
        {
            InitializeComponent();
        }

        private void InitTask(Task data)
        {
            _data = data;
            if (data != null)
            {
                var txt = data.Desc;
                linkTask.Text = txt.Length > 25 ? txt.Substring(0, 25) + "..." : txt;
                chkDone.Checked = data.IsCompleted;
            }
        }

        public static TaskControl CreateTask(Task data)
        {
            var task = new TaskControl();
            task.InitTask(data);
            return task;
        }

        private void OnCheckedChanged(object sender, EventArgs e)
        {
            _data.IsCompleted = chkDone.Checked;
            if (OnTaskDone != null)
                OnTaskDone(_data);
        }

        private void OnLinkClick(object sender, EventArgs e)
        {
            if (OnTaskClick != null)
            {
                OnTaskClick(_data);
            }
        }

        private void linkTask_MouseDown(object sender, MouseEventArgs e)
        {
            DoDragDrop(_data, DragDropEffects.Copy);
        }

        private void linkTask_DragOver(object sender, DragEventArgs e)
        {
           e.Effect = DragDropEffects.Move;
        }
    }
}
